/**
 * TMP001显示
 * */


var formList = {}; //表单对象
var commentList = {
    data: [], //列表数据
    total: 0, //记录总数
    pages: {
        start: 0, //起始页0
        limit: 10 //每页记录数10
    }
}; //评论数据
var historyList = []; //历史数据
var followList = []; //关注数据


//获取一条数据
function getOne(){
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '没有登录'});
        return
    }
    if (code == '' || code == undefined) {
        msgPop({msg: '码值错误'});
        return
    }
    if (tmpId == undefined || tmpId == '') {
        msgPop({msg: 'ID错误'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        tmpId: tmpId,
        userId: userInfo.userId
    };
    $.ajax({
        type: apis.tmp1One.method,
        url: domain + apis.tmp1One.url,
        data: formData,
        //headers: {
        //    'Content-Type': 'application/json; charset=utf-8',
        //    'code': code,
        //    'userid': userInfo.userId
        //},
        //beforeSend: function (xhr) {
        //    xhr.setRequestHeader('Content-Type', 'application/json');
        //    xhr.setRequestHeader('code', code);
        //    xhr.setRequestHeader('userid', userInfo.userId);
        //},
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            formList = res.data;
            showHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//场景拼接字符串
function showHtml(){
    var h = '', i;
    //图片
    h += '<style type="text/css">';
    h += '#showImgOut {width: ' + cw + 'px; height: ' + cw * 0.5 + 'px; overflow: hidden;}';
    h += '#showImgOut img {width: ' + cw + 'px;}';
    h += '#followBtn {padding: 3px 10px; border: 1px solid #0088cc; color: #0088cc; border-radius: 5px;}';
    h += '</style>';
    h += '<div id="showImgOut">';
    h += '<img src="' + formList.imgList[0].link + '"/>';
    h += '<div id="showImgMore" onclick="bigImgs();">点击更多</div>';
    h += '</div>';
    //标签名称
    h += '<table>';
    h += '<tr>';
    h += '<td colspan="2" style="text-align: center;">';
    h += '<span id="followBtn" onclick="setFollow();">';
    if (formList.isfollow == 0) {
        h += '未关注？点击关注';
    } else {
        h += '已关注？点击取消';
    }
    h += '</span>';
    h += '</td>';
    h += '<tr>';
    h += '<td class="tl">标签名称</td>';
    h += '<td class="tr">';
    h += formList.title;
    h += '</td>';
    h += '</tr>';
    //自定义属性
    for (i = 0; i < formList.attrList.length; i++) {
        h += '<tr>';
        h += '<td class="tl">' + formList.attrList[i].attrName + '</td>';
        h += '<td class="tr">' + formList.attrList[i].attrVal + '</td>';
        h += '</tr>';
    }
    h += '</table>';
    $('#form').html(h);
    $('#header-title').html(formList.title);
}


//历史详情
function bigImgs(){
    var imgs = formList.imgList;
    if (imgs.length == 0) {
        msgPop({msg: '没有图片'}); return
    }
    var h = '', i;
    h += '<div style="position: fixed; top: 35px; z-index: 100;">';
    h += '<div style="width: '+cw+'px; height: ' + (ch - 35) + 'px; background: #000; opacity: 0.5; position: fixed; top: 35px; z-index: 100;"></div>';
    h += '<div style="width: ' + cw + 'px; height: ' + (ch - 35) + 'px; background: #fff; overflow: auto; position: fixed; top: 35px; z-index: 100;">';
    h += '<div>';
    for (i = 0; i < imgs.length; i ++) {
        h += '<img src="' + imgs[i].link + '" style="margin: 10px;"/>';
    }
    h += '</div>';
    h += '<div><button style="margin: 20px 10%; padding: 10px 0; width: 80%; border: 0; border-radius: 3px; background: coral; color: #fff;" onclick="$(\'#bigImgPop\').html(\'\');">确定</button></div>';
    h += '</div>';
    h += '</div>';
    $('#bigImgPop').html(h);
}


//广告位
function getAdvert(){
    var h = '<img src="' + domain + '/videos/2024/20240202.png" style="width: 100%; opacity: 0.2;"/>';
    $('#advert').html(h);
}


//互动
function getHudong(){
    var h = '';
    //菜单
    h += '<div id="ul" style="width: ' + cw + 'px;">';
    h += '<div id="m-1" class="click" onclick="changeMenu(1)";>评论 <span id="m-1-total"></span></div>';
    h += '<div id="m-2" onclick="changeMenu(2);">版主 <span id="m-2-total"></span></div>';
    h += '<div id="m-3" onclick="changeMenu(3);">关注 <span id="m-3-total"></span></div>';
    //h += '<div>扫码</div>';
    h += '</div>';
    //内容评论
    h += '<div class="mbody" id="mbody-1">';
    //h += '<table>';
    //h += '<tr>';
    //h += '<td class="pl-l">';
    //h += '<img src="../../../imgs/mpPlay.png"/>';
    //h += '<div>用户名</div>';
    //h += '</td>';
    //h += '<td class="pl-r">';
    //h += '<div style="color: #ccc;">2024-02-02</div>';
    //h += '<div style="width: ' + (cw * 1 - 80) + 'px; max-height: 50px; word-wrap: break-word; overflow: hidden;">11111111111111111111111111111111122222222222222222222222</div>';
    //h += '</td>';
    //h += '</tr>';
    //h += '</table>';
    h += '<table><tr><td colspan="2" style="width: ' + cw + 'px; text-align: center; font-size: 12px; color: #888;">没有评论</td></tr></table>';
    h += '<div class="div-hr"></div>';
    h += '<div id="addbtn"><input type="text" id="pl-txt" placeholder="有爱评论，说点儿好听的" value=""/><a href="javascript:;" onclick="addComment();">发布评论</a></div>';
    h += '</div>';
    //内容版主
    h += '<div class="mbody" id="mbody-2" style="display: none;">';
    //h += '<table>';
    //h += '<tr>';
    //h += '<td class="bz-l">标签名称</td>';
    //h += '<td class="bz-m">标签</td>';
    //h += '<td class="bz-r" rowspan="3">点击更多</td>';
    //h += '<tr>';
    //h += '<td class="bz-l">属性</td>';
    //h += '<td class="bz-m">属性值</td>';
    //h += '<tr>';
    //h += '<td class="bz-l bzhr">更新时间</td>';
    //h += '<td class="bz-2 bzhr">2024-02-02</td>';
    //h += '</tr>';
    //h += '</table>';
    h += '</div>';
    //关注
    h += '<div class="mbody" id="mbody-3" style="display: none;">';
    h += '<table><tr>';
    h += '<td colspan="2" style="width: ' + cw + 'px; text-align: center; font-size: 12px; color: #888;">';
    h += '没有用户';
    h += '</td></td></table>';
    h += '</div>';
    $('#hudong').html(h);
    getCommentList();
    getTmpHistoryList();
    getFollowList();
}


//互动切换
function changeMenu(v){
    $('#ul > div').removeClass('click');
    $('#m-' + v).addClass('click');
    $('.mbody').hide();
    $('#mbody-' + v).show();
}


//获取评论
function getCommentList(){
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '没有登录'});
        return
    }
    if (tmpId == undefined || tmpId == '') {
        msgPop({msg: 'ID错误'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        tmpId: tmpId,
        pageStart: commentList.pages.start,
        pageLimit: commentList.pages.limit
    };
    //console.log(formData);
    $.ajax({
        type: apis.tmp1CommentIndex.method,
        url: domain + apis.tmp1CommentIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            for (var i = 0; i < res.data.data.length; i ++) {
                commentList.data.push(res.data.data[i]);
            }
            commentList.total = res.data.total;
            if (res.data.data.length == commentList.pages.limit) {
                commentList.pages.start ++;
            }
            getCommentHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//评论HTML
function getCommentHtml(){
    var total = commentList.total;
    var d = commentList.data;
    $('#m-1-total').html(total);
    var h = '', id, addDates, sender, accept;
    for (var i = 0; i < d.length; i ++) {
        id = d[i].id;
        sender = d[i].userId;
        accept = d[i].acceptId;
        addDates = d[i].addtime.split(' ');
        h += '<table>';
        h += '<tr>';
        h += '<td class="pl-l">';
        h += '<img src="../../../imgs/mpPlay.png"/>';
        h += '<div>' + strSuffix(sender, 5) + '</div>';
        h += '</td>';
        h += '<td class="pl-r" onclick="aiteComment(' + id + ');">';
        h += '<div>';
        h += '对<b id="accept-' + id + '">';
        h += userInfo.userId == sender ? '版主' : strSuffix(accept, 6);
        h += '</b>：';
        h += '<span style="color: #ccc; float: right;">' + addDates[0] + '</span>';
        h += '</div>';
        h += '<div style="width: ' + (cw * 1 - 80) + 'px; max-height: 50px; word-wrap: break-word; overflow: hidden;">' + d[i].content + '</div>';
        h += '<input type="hidden" id="acceptId' + id + '" value="' + accept + '"/>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
        h += '<div class="div-hr"></div>';
    }
    h += '<div style="margin-bottom: 10px; padding: 10px 0; text-align: center; font-size: 12px; color: #888;" onclick="getCommentList();">点击更多</div>';
    h += '<div style="height: 40px;"></div>';
    h += '<div id="addbtn"><input type="text" id="pl-txt" placeholder="有爱评论，说点儿好听的" value=""/><a href="javascript:;" onclick="addComment();">发布评论</a></div>';
    h += '<input type="hidden" id="acceptCurrId" value=""/>';
    $('#mbody-1').html(h);
}


//评论艾特新增
function aiteComment(id){
    var accept = $('#accept-' + id).html();
    var acceptId = $('#acceptId' + id).val();
    console.log(id, accept, acceptId);
    $('#pl-txt').attr('placeholder', '回复@' + accept);
    $('#acceptCurrId')[0].value = acceptId;
}


//评论新增
function addComment(){
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '没有登录'});
        return
    }
    if (tmpId == undefined || code == '') {
        msgPop({msg: 'ID错误'});
        return
    }
    var acceptCurrId = $('#acceptCurrId').val();
    var content = $('#pl-txt').val();
    if (content == '') {
        msgPop({msg: '评论没有填写'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        tmpId: tmpId,
        acceptId: acceptCurrId,
        content: content
    };
    $.ajax({
        type: apis.tmp1CommentAdd.method,
        url: domain + apis.tmp1CommentAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '发布成功'});
            var newList = [res.data];
            for (var i = 0; i < commentList.data.length; i ++) {
                newList[i + 1] = commentList.data[i];
            }
            commentList.data = newList;
            commentList.total ++;
            getCommentHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//获取关注列表
function getFollowList(){
    var h = '';
    h += '<table><tr>';
    h += '<td colspan="2" style="width: ' + cw + 'px; text-align: center; font-size: 12px; color: #888;">';
    h += '没有用户关注';
    h += '</table>';
    $('#mbody-3').html(h);
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '没有登录'});
        return
    }
    if (tmpId == undefined || tmpId == '') {
        msgPop({msg: '物品ID错误'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        tmpId: tmpId,
        userId: userInfo.userId
    };
    console.log(formData);
    $.ajax({
        type: apis.tmp1FollowIndex.method,
        url: domain + apis.tmp1FollowIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            followList = res.data;
            getFollowHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//关注列表拼接字符串
function getFollowHtml(){
    var h = '', i, uname, addtimes;
    for (i = 0; i < followList.length; i ++) {
        uname = followList[i].user_id;
        addtimes = followList[i].addtime.split(' ');
        h += '<table>';
        h += '<tr>';
        h += '<td class="gz-l">用户</td>';
        h += '<td class="gz-m">' + uname + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="gz-l gzhr">关注时间</td>';
        h += '<td class="gz-m gzhr">' + addtimes[0] + '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#mbody-3').html(h);
    $('#m-3-total').html(followList.length);
}


//设置关注
function setFollow(v){
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '没有登录'});
        return
    }
    if (tmpId == undefined || tmpId == '') {
        msgPop({msg: '物品ID错误'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        tmpId: tmpId,
        userId: userInfo.userId
    };
    console.log(formData);
    $.ajax({
        type: apis.tmp1FollowModify.method,
        url: domain + apis.tmp1FollowModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//获取修改历史
function getTmpHistoryList(){
    if (!userInfo || !userInfo.userId) {
        msgPop({msg: '没有登录'});
        return
    }
    if (tmpId == undefined || code == '') {
        msgPop({msg: 'ID错误'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        tmpId: tmpId,
        pageStart: 0,
        pageLimit: 10
    };
    console.log(formData);
    $.ajax({
        type: apis.tmp1HistoryIndex.method,
        url: domain + apis.tmp1HistoryIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            historyList = res.data;
            getHistoryHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改历史拼接字符串
function getHistoryHtml(){
    var h = '', i,
        title, attrs, attrName, attrVal, addtime;
    for (i = 0; i < historyList.length; i ++) {
        title = historyList[i].title;
        attrs = historyList[i].attrs;
        addtime = historyList[i].addtime;
        attrName = attrs.length > 0 ? attrs[0].attr_name : '';
        attrVal = attrs.length > 0 ? attrs[0].val : '';
        h += '<table>';
        h += '<tr>';
        h += '<td class="bz-l">标签名称</td>';
        h += '<td class="bz-m">' + title + '</td>';
        h += '<td class="bz-r" rowspan="3" onclick="historyShow(' + historyList[i].id + ');">点击更多</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="bz-l">' + attrName + '</td>';
        h += '<td class="bz-m">' + attrVal + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="bz-l bzhr">更新时间</td>';
        h += '<td class="bz-2 bzhr">' + addtime + '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#mbody-2').html(h);
}


//历史详情
function historyShow(id){
    var h = '', i, hisOne = '', attrs, imgs;
    for (i = 0; i < historyList.length; i ++) {
        if (historyList[i].id == id) {
            hisOne = historyList[i];
        }
    }
    if (hisOne.length == 0) {
        msgPop({msg: '没有该记录'});
    }
    imgs = hisOne.imgs;
    attrs = hisOne.attrs;
    h += '<style type="text/css">.his-tb {margin: 0 5%; margin-top: 50px; width: 90%; border-collapse: collapse; } .his-tb td {padding: 10px; border: 1px solid #eee; font-size: 14px;} .his-tb .tdl {width: 30%; text-align: right; color: #888;} .his-tb .tdr{width: 60%;} .tdr img {margin: 5px 5%; width: ' + cw * 0.5 + 'px;}</style>';
    h += '<div style="position: fixed; top: 35px; z-index: 100;">';
    h += '<div style="width: '+cw+'px; height: ' + (ch - 35) + 'px; background: #000; opacity: 0.5; position: fixed; top: 35px; z-index: 100;"></div>';
    h += '<div style="width: ' + cw + 'px; height: ' + (ch - 35) + 'px; background: #fff; overflow: auto; position: fixed; top: 35px; z-index: 100;">';
    h += '<table class="his-tb">';
    h += '<tr>';
    h += '<td class="tdl">图片</td>';
    h += '<td class="tdr">';
    if (imgs.length == 0) {
        h += '没有图片';
    } else {
        for (i = 0; i < imgs.length; i ++) {
            h += '<img src="' + imgs[i] + '"/>';
        }
    }
    h += '</td>';
    h += '</tr>';
    h += '<tr>';
    h += '<td class="tdl">标签名称</td>';
    h += '<td class="tdr">' + hisOne.title + '</td>';
    h += '</tr>';
    if (attrs.length > 0) {
        for (i = 0; i < attrs.length; i ++) {
            h += '<tr>';
            h += '<td class="tdl">' + attrs[i].attr_name + '</td>';
            h += '<td class="tdr">' + attrs[i].val + '</td>';
            h += '</tr>';
        }
    }
    h += '<tr>';
    h += '<td class="tdl">时间</td>';
    h += '<td class="tdr">' + hisOne.addtime + '</td>';
    h += '</tr>';
    h += '</table>';
    h += '<div><button style="margin: 20px 10%; padding: 10px 0; width: 80%; border: 0; border-radius: 3px; background: coral; color: #fff;" onclick="$(\'#hisPop\').html(\'\');">确定</button></div>';
    h += '</div>';
    h += '</div>';
    $('#hisPop').html(h);
}